package com.guldan.jaina.provider;

import com.guldan.core.dubbo.client.ResponseDTO;
import com.guldan.core.search.Pagination;
import com.guldan.jaina.dto.gauthority.GAuthorityDTO;
import com.guldan.jaina.dto.users.*;
import com.guldan.jaina.manager.UserManager;
import net.sf.json.JSONArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by Silas.
 * Date: 2016/11/18
 * Time: 23:43
 */
@Component
public class UserProviderImpl implements IUserProvider {

    @Autowired
    private UserManager userManager;

    public Logger logger = LoggerFactory.getLogger(getClass());


    //分页获取用户列表
    @Override
    public ResponseDTO<Pagination<UsersDTO>> userListByPage(UsersSearchDTO var1) {
        ResponseDTO<Pagination<UsersDTO>> responseDTO = new ResponseDTO<>();
        responseDTO.setData(userManager.list(var1));
        return responseDTO;
    }

    //获取某个用户的信息，传入：userid
    @Override
    public ResponseDTO<UsersDTO> userGet(Integer var1) {
        ResponseDTO<UsersDTO> responseDTO = new ResponseDTO<>();
        responseDTO.setData(userManager.get(var1));
        return responseDTO;
    }

    //新增/保存某个用户的信息
    @Override
    public ResponseDTO<UsersDTO> userSave(UsersDTO var1) {
        ResponseDTO<UsersDTO> responseDTO = new ResponseDTO<>();

        try {
            responseDTO.setData(userManager.save(var1));
        } catch (Exception ex) {
            responseDTO.setStatus(false);
            responseDTO.setException(ex);
        }
        return responseDTO;
    }

    //获取某个用户的角色列表
    @Override
    public ResponseDTO<List<UserRoleDTO>> userRoleList(Integer var1) {
        ResponseDTO<List<UserRoleDTO>> responseDTO = new ResponseDTO<>();
        responseDTO.setData(userManager.userRoleList(var1));
        return responseDTO;
    }

    //删除某个用户的角色，传入：userid，roleId
    @Override
    public ResponseDTO<List<UserRoleDTO>> userDeleteRole(Integer var1, Integer var2) {
        ResponseDTO<List<UserRoleDTO>> responseDTO = new ResponseDTO<>();
        responseDTO.setData(userManager.userDeleteRole(var1, var2));
        return responseDTO;
    }

    //修改某个用户的角色列表
    @Override
    public ResponseDTO<List<UserRoleDTO>> userSaveRole(UserRoleSaveDTO var1) {
        ResponseDTO<List<UserRoleDTO>> responseDTO = new ResponseDTO<>();
        responseDTO.setData(userManager.userSaveRole(var1));
        return responseDTO;
    }

    //修改密码接口
    @Override
    public ResponseDTO<Boolean> userUpdatePwd(UserPwdUpdateDTO var1) {
        ResponseDTO<Boolean> responseDTO = new ResponseDTO<>();
        responseDTO.setData(userManager.userUpdatePwd(var1));
        return responseDTO;
    }

    //删除某个用户接口
    @Override
    public ResponseDTO<Boolean> userDelete(Integer var1) {
        ResponseDTO<Boolean> responseDTO = new ResponseDTO<>();
        responseDTO.setData(userManager.userDelete(var1));
        return responseDTO;
    }

   /* //用户登录接口
    @Override
    public ResponseDTO<UsersDTO> login(String userAccount, String password, String JSESSIONID) {
        ResponseDTO<UsersDTO> responseDTO = new ResponseDTO<>();
        responseDTO.setData(userManager.login(userAccount, password, JSESSIONID));
        return responseDTO;
    }

    //通过用户id获取用户所授权树形菜单
    @Override
    public ResponseDTO<List<GAuthorityDTO>> getMenuByUserId(Integer userId) {
        ResponseDTO<List<GAuthorityDTO>> responseDTO = new ResponseDTO<>();
        responseDTO.setData(userManager.getMenuByUserId(userId));
        return responseDTO;
    }

    //通过登录用户JSESSIONID获取用户所授权树形菜单
    @Override
    public ResponseDTO<List<GAuthorityDTO>> getMenuByJSESSIONID(String JSESSIONID) {
        ResponseDTO<List<GAuthorityDTO>> responseDTO = new ResponseDTO<>();
        responseDTO.setData(userManager.getMenuByJSESSIONID(JSESSIONID));
        return responseDTO;
    }

    //判断某个JSESSIONID是否已经登录
    @Override
    public ResponseDTO<UserCacheDTO> authentication(String JSESSIONID) {
        ResponseDTO<UserCacheDTO> responseDTO = new ResponseDTO<>();
        responseDTO.setData(userManager.authentication(JSESSIONID));
        return responseDTO;
    }*/

    //获取某个用户的登录缓存实体
    @Override
    public ResponseDTO<UserCacheDTO> getUserCache(String JSESSIONID) {
        ResponseDTO<UserCacheDTO> responseDTO = new ResponseDTO<>();
        responseDTO.setData(userManager.getUserCache(JSESSIONID));

        logger.info("json5:" + JSONArray.fromObject(responseDTO).toString());

        return responseDTO;
    }

}
